package Client.GUI;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Client.logic.ClientIF;
import Server.DataBase.Parent;
import Server.GUI.MyJTable;
import Server.Message.MessageGetParent;
import Server.Message.MessageGetParentReplay;

public class BlockParentsPanel extends MyJPanel {

private static final long serialVersionUID = 1L;
	
	private ArrayList<Parent> arrayUnblockedParent =null;
	private ArrayList<Parent> arrayBlockedParent =null;
	private ArrayList<Parent> arrayBlock =null;
	private ArrayList<Parent> arrayUnblock =null;
	private MyJTable table;
	private MyJTable table2;
	private ParentTableModel ptm;
	private ParentTableModel ptm2;
	private JButton btnBlock;
	private JButton btnUnblock;
	private JButton btnApply;
	private JButton btnCancel;
	
	public BlockParentsPanel( ClientIF client) {
		super(PanelType.BLOCK_PARENTS_PANEL, client);
		
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"block parents.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(12, 13, 637, 99);
			add(lblAddClass);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setLayout(null);
		
		
		init();
	}
	
	public void initArrays(){
		
		arrayUnblockedParent = new ArrayList<Parent>();
		arrayBlockedParent = new ArrayList<Parent>();
		arrayBlock = new ArrayList<Parent>();
		arrayUnblock = new ArrayList<Parent>();
		
		getClient().sendMsgToServer(new MessageGetParent());
		MessageGetParentReplay msg2 = (MessageGetParentReplay) getClient().getMessageFromServer();
		arrayUnblockedParent = msg2.getUnblock();
		arrayBlockedParent = msg2.getBlock();
	}
	
	public void initTables(){
		ptm = new ParentTableModel();		
		ptm.setArray(arrayUnblockedParent);
		table = new MyJTable(ptm);
		table.setBounds(10, 158, 249, 206);
		table.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
 			public void valueChanged(ListSelectionEvent e) {
 				table.selectedRow = table.getTable().getSelectedRow();
 				btnBlock.setEnabled(true);
 			}
         });
		table.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		table.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		add(table);
		
		ptm2 = new ParentTableModel();
		ptm2.setArray(arrayBlockedParent);
		table2 = new MyJTable(ptm2);
		table2.setBounds(393, 158, 249, 206);
		table2.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
 			public void valueChanged(ListSelectionEvent e) {
 				table2.selectedRow = table2.getTable().getSelectedRow();
 				btnUnblock.setEnabled(true);
 			}
         });
		table2.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		table2.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		add(table2);
	}
	
	public void initBtn(){
		btnBlock = new JButton("Block");
		btnBlock.setBounds(269, 210, 107, 23);
		btnBlock.setEnabled(false);
		btnBlock.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int row = table.getSelectedItem();
				Parent parent = arrayUnblockedParent.remove(row);
				arrayBlock.add(parent);
				arrayBlockedParent.add(parent);
				ptm.setArray(arrayUnblockedParent);
				ptm2.setArray(arrayBlockedParent);
				btnBlock.setEnabled(false);
				btnApply.setEnabled(true);
				btnCancel.setEnabled(true);
			}
		});
		add(btnBlock);
		
		btnUnblock = new JButton("Unblock");
		btnUnblock.setBounds(269, 266, 107, 23);
		btnUnblock.setEnabled(false);
		btnUnblock.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Parent parent = arrayBlockedParent.remove(table2.getSelectedItem());
				int i=0;
				boolean next = true;
				arrayUnblockedParent.add(parent);
				while(next){
					if (i<arrayBlock.size()){
						if (arrayBlock.get(i).getIdUser()==parent.getIdUser()){
							arrayBlock.remove(i);
							next=false;
						}
					}
					else {
						arrayUnblock.add(parent);
						next=false;
					}
					i++;
				}
				ptm.setArray(arrayUnblockedParent);
				ptm2.setArray(arrayBlockedParent);
				btnUnblock.setEnabled(false);
				btnApply.setEnabled(true);
				btnCancel.setEnabled(true);
			}
		});
		add(btnUnblock);
		
		btnApply = new JButton("Apply");
		btnApply.setBounds(196, 416, 89, 23);
		btnApply.setEnabled(false);
		btnApply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getClient().sendMsgToServer(new MessageGetParentReplay(arrayBlock, arrayUnblock));
				initArrays();
				btnBlock.setEnabled(false);
				btnUnblock.setEnabled(false);
				btnApply.setEnabled(false);
				btnCancel.setEnabled(false);
			}
		});
		add(btnApply);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setBounds(365, 416, 89, 23);
		btnCancel.setEnabled(false);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getClient().swapFromBack(new BlockParentsPanel(getClient()));
				}
			});
		add(btnCancel);
		
		
		
	
	}
	
	public void init(){
		initArrays();
		
		JLabel lblBlockedParents = new JLabel("Blocked parents");
		lblBlockedParents.setBounds(474, 129, 126, 16);
		add(lblBlockedParents);
		
		JLabel lblUnblockedParents = new JLabel("Unblocked parents");
		lblUnblockedParents.setBounds(66, 129, 126, 16);
		add(lblUnblockedParents);
		
		initTables();
		initBtn();
	}

	@Override
	public MyJPanel pushPanel() {
		return new BlockParentsPanel(getClient());
	}
}
